<?php

class CAuthClass extends systemBase {

/*
	public function __construct($lsModuleName) {
		parent::__construct($lsModuleName);
	}
	*/
	public function actionNew() {
		global $_config;
		$lsError = null;
		$lrNewUser = array();
		$lsCreated = "N";
		if(IsFormSent("newuser")) {
			//
			$lrNewUser["password1"] = mandatory(getPost("password1"), "Heslo", $lsError);
			$lrNewUser["password2"] = mandatory(getPost("password2"), "Potvrzení hesla", $lsError);
			//$lrNewUser["username"]  = mandatory(getPost("username"),  "Uživatelské jméno", $lsError);
			$lrNewUser["username"]  = null;
			$lrNewUser["email"]     = mandatory(getPost("email"),     "E-mail", $lsError);
			$this->addError($lsError, false);
			//
			if(!isEmailValid($lrNewUser["email"])) {
				$this->addError("Neplatný email");
			}
			if($lrNewUser["password1"] != $lrNewUser["password2"]) {
				$this->addError("Hesla se neshodují");
			}
			if(!$this->IfError()) {
				if(strlen($lrNewUser["password1"]) < $_config["Auth"]["MinPasswordLength"]) {
					$this->addError("Heslo je příliš krátké");
				} else {
					$lrNewUser["password"] = generatePassword($lrNewUser["password1"]);
				}
			}
			// nenastala zadna chyba?
			if(!$this->IfError()) {
				$lsResult = $this->Model->NewUser($lrNewUser);
				if($lsResult == "OK") {
					$this->addInfo("Vytvořeno!");
					$lsCreated = "Y";
				} elseif($lsResult == "USEREXISTS") {
					$this->addError("Tento uživatel již existuje");
				} elseif($lsResult == "EMAILEXISTS") {
					$this->addError("Tento e-mail je již registrován");
				} elseif($lsResult == "ERROR") {
					$this->addError("Při registraci nastala chyba");
				}
			}
		}
		$this->Smarty->assign("email", NVL(getPost("email"),"@"));
		$this->Smarty->assign("username", getPost("username"));
		$this->Smarty->assign("created", $lsCreated);
	}

	public function actionLogin() {
		global $_config;
		$lsError = null;
		//
		$this->Smarty->assign("show", "form");
		$this->Smarty->assign("ModuleName", "Přihlášení");
		$this->Smarty->assign("LoginType", $_config["Auth"]["LoginType"]);
		if(!isLogged()) {
			$this->Smarty->assign("username", getPost("username"));
			$this->Smarty->assign("email", NVL(getPost("email"),"@"));
			// je odeslan formular
			if(IsFormSent("login")) {
				$lsPassword = mandatory(getPost("password"),"Heslo",$lsError);
				if($_config["Auth"]["LoginType"] == AUTH_LOGINTYPE_USER) {
					$lsUsername = mandatory(getPost("username"),"Uživatel", $lsError);
				}
				elseif ($_config["Auth"]["LoginType"] == AUTH_LOGINTYPE_EMAIL) {
					$lsEmail = mandatory(getPost("email"),"E-Mail", $lsError);
				}
				$this->AddError($lsError,false);
				$lsPassword = generatePassword($lsPassword);
				if(!$this->IfError()) {
					if($_config["Auth"]["LoginType"] == AUTH_LOGINTYPE_USER) {
						$lsResult = $this->Model->UserLogin($lsUsername, $lsPassword);
					} elseif ($_config["Auth"]["LoginType"] == AUTH_LOGINTYPE_EMAIL) {
						$lsResult = $this->Model->EmailLogin($lsEmail, $lsPassword);
					}
					if($lsResult) {
						// uzivatel je prihlasen
						$lrLogin = array();
						//$lrLogin["username"] = $lsResult["username"];
						$lrLogin["email"] = $lsEmail;
						$lrLogin["IDUser"] = $lsResult["iduser"];
						$lrLogin["level"] = $lsResult["level"];
						$lrLogin["logged"] = "Y";
						setSession("Login", $lrLogin);
						redirect(PATH_ROOT);
					} else {
						// spatne jmeno / heslo
						$this->addError("Přihlášení - špatné jméno / heslo");
					}
				}
			}

		}
	}

	public function actionLogout() {
		deleteSession("Login");
		redirect(getURL());
	}

}